import 'package:flutter/material.dart';

class AnimationDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AnimationDemo"),
        elevation: 0.0,
      ),
      body: AnimationDemoHome(),
    );
  }
}

class AnimationDemoHome extends StatefulWidget {
  @override
  _AnimationDemoHomeState createState() => _AnimationDemoHomeState();
}

class _AnimationDemoHomeState extends State<AnimationDemoHome> 
  with TickerProviderStateMixin{

  AnimationController animationController;
  Animation animation;
  Animation animationColor;
  CurvedAnimation curve;

  @override
  void initState() { 
    super.initState();
    
    animationController = AnimationController(
      // lowerBound: 32.0,
      // upperBound: 100.0,
      // value: 32.0,
      duration: Duration(milliseconds: 3000),
      vsync: this,
    );

    curve = CurvedAnimation(parent: animationController, curve: Curves.bounceOut,);

    animation = Tween(begin: 32.0, end: 100.0).animate(curve);
    animationColor = ColorTween(begin: Colors.red, end: Colors.red[900]).animate(curve);

    animationController.addListener(
      () { 
        print("${animationController.value}");
        setState(() {});
      }
    );

    // animationController.addStatusListener((status) {
    //   print(status);
    // });

    // animationController.forward();
  }

  @override
  void dispose() { 
    super.dispose();
    animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedHeart(
        animations: [
          animation,
          animationColor
        ],
        controller: animationController,
      ),
    );
  }
}

class AnimatedHeart extends AnimatedWidget {

  final List animations;
  final AnimationController controller;

  AnimatedHeart({
    this.animations,
    this.controller,
  }) : super(listenable: controller);

  @override
  Widget build(BuildContext context) {
    return IconButton(
        icon: Icon(Icons.favorite),
        iconSize: animations[0].value,
        color: animations[1].value,
        onPressed: () {
          switch(controller.status) {
            case AnimationStatus.completed:
              controller.reverse();
              break;
            default:
              controller.forward();
          }
        },
      );
  }

}